package cn.yzx.pethome.system.login.controller;

import cn.yzx.pethome.constants.BaseConstants;
import cn.yzx.pethome.system.login.domain.EmailRegister;
import cn.yzx.pethome.system.login.service.IEmailService;
import cn.yzx.pethome.system.logininfo.domain.Logininfo;
import cn.yzx.pethome.system.logininfo.service.ILogininfoService;
import cn.yzx.pethome.system.user.domain.User;
import cn.yzx.pethome.system.user.service.IUserService;
import cn.yzx.pethome.utils.AjaxResult;
import cn.yzx.pethome.utils.MD5Utils;
import cn.yzx.pethome.utils.StrUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/register")
//这里是说明你这个类是干什么用的
@Api(value = "注册API",description="注册的功能")
public class RegisterController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IUserService userService;
    @Autowired
    private ILogininfoService logininfoService;
    @Autowired
    private IEmailService emailService;
    @PostMapping("/email")
    public AjaxResult registerEmail(@RequestBody EmailRegister emailRegister){
        //邮箱正则表达式
        String regex = "[a-zA-Z0-9_]+@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9]+)+";
        if (emailRegister.getEmail() == null){
            return AjaxResult.error().setMsg("邮箱不能为空");
        }else if (!emailRegister.getEmail().matches(regex)){
            return AjaxResult.error().setMsg("邮箱格式错误");
        }
        if (emailRegister.getImageCode() == null){
            return AjaxResult.error().setMsg("图片验证码不能为空");
        }
        if (emailRegister.getPassword() == null){
            return AjaxResult.error().setMsg("密码不能为空");
        }
        if (emailRegister.getPasswordRepeat() == null){
            return AjaxResult.error().setMsg("确认密码不能为空");
        }
        if (!emailRegister.getPassword().equals(emailRegister.getPasswordRepeat())){
            return AjaxResult.error().setMsg("两次密码不一致");
        }
        //判断验证码是否有效
        Object o = redisTemplate.opsForValue().get(emailRegister.getImageCodeKey());
        if (o == null){
            return AjaxResult.error().setMsg("验证码已失效");
        }
        //判断验证码是否正确
        if (!o.toString().equals(emailRegister.getImageCode())){
            return AjaxResult.error().setMsg("验证码错误");
        }
        //判断邮箱是否被注册
        User user = userService.findByEmail(emailRegister.getEmail());
        if (user != null){
            return AjaxResult.error().setMsg("该邮箱已被注册");
        }
        //获取盐值
        String salt = StrUtils.getComplexRandomString(32);
        //密码加密
        String psw = MD5Utils.encrypByMd5(emailRegister.getPassword() + salt);
        User user1 = new User();
        user1.setEmail(emailRegister.getEmail());
        user1.setCreatetime(new Date());
        user1.setSalt(salt);
        user1.setPassword(psw);
        user1.setState(BaseConstants.User.STATE_NO_ACTIVE);
        userService.add(user1);
        //数据同步
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user1,logininfo);
        logininfo.setDisable(BaseConstants.Logininfo.DISABLE_0);
        logininfo.setType(BaseConstants.Logininfo.STATE_USER);
        logininfoService.add(logininfo);
        user1.setLogininfoId(logininfo.getId());
        userService.update(user1);
        //发送邮件
        System.out.println(user1.getId());
        emailService.sendActMail(emailRegister.getEmail(),user1.getId());
        return AjaxResult.success().setMsg("注册成功请去邮箱激活");
    }
}
